package de.gameduell.framework.game.billard.view {
	import de.gameduell.framework.application.protocol.MultiplayerProtocol;
	import de.gameduell.framework.chat.ChatBubble;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.Player;
	import de.gameduell.framework.game.billard.controller.BillardProtocol;
	import de.gameduell.framework.game.billard.controller.IMultiplayerBillardController;
	import de.gameduell.framework.game.billard.controller.PhysicController;
	import de.gameduell.framework.game.billard.event.PhysicEvent;
	import de.gameduell.framework.game.billard.model.BillardPlayer;
	import de.gameduell.framework.game.watch.StopWatch;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.framework.util.Audio;
	import de.gameduell.framework.util.Library;
	import de.gameduell.net.src.communication.ServerMessageEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;	

	/**
	 * @author cbra
	 */
	public class PlayerView {
		
		private const CHAT_BUBBLE_LIFETIME:int = 3000;	
		
		private var timeline:Sprite;
		private var avatar:Sprite;
		private var name:TextField;
		private var controller:IMultiplayerBillardController;
		private var time:TextField;
		private var content:Sprite;
		private var player:BillardPlayer;
		private var singleStopWatch:StopWatch;
		private var physicController:PhysicController;
		private var color:MovieClip;
		private var audio:Audio;	
		private var lastTenSeconds:Timer;
		private var highlight:Sprite;
		private var locale:Locale;
		private var library:Library;
		private var pocketedballTube:BallTube;

		public function PlayerView(aContent:Sprite,id:String,aController:IMultiplayerBillardController, aPlayer:BillardPlayer) {		
			
			controller = aController;
			physicController = controller.physicController;
			singleStopWatch = controller.stopWatch;
			audio = Audio.getInstance();
			locale = Locale.getInstance();
			library = Library.getInstance();
					
			content	  = aContent;
			timeline  = content.getChildByName(id) as Sprite;
			highlight = timeline.getChildByName("highlight") as Sprite;
			avatar    = timeline.getChildByName("avatar") as Sprite;
			name 	  = timeline.getChildByName("playerName") as TextField;
			time	  = timeline.getChildByName("shotTime") as TextField;
			color	  = timeline.getChildByName("color") as MovieClip;
			player	  = aPlayer;

			time.visible = false;
			highlight.visible = false;
			name.text = player.shortName;
					
			lastTenSeconds = new Timer(1000,10);
			lastTenSeconds.addEventListener(TimerEvent.TIMER, warningSoundLoop);
			pocketedballTube = new BallTube(timeline,controller,true);
			
 			createBubble();
			
			player.addEventListener(Player.ACTIVE_CHANGE, onActiveChange);
			player.addEventListener(Player.AVATAR, onAvatar);
			controller.addEventListener(BillardProtocol.HIT_RESULTS_SYNCHRONIZED, onHitResultSynchronized);
			controller.addEventListener(BillardProtocol.PLAYER_COLOR, onPlayerColor);
			controller.addEventListener(MultiplayerProtocol.GO, onGo);
			physicController.addEventListener(PhysicEvent.CALCULATION_START, onQueueShot);
		}
		
		private function createBubble():void{
			var bubble:Sprite = content.getChildByName(player.isUser?"userBubble":"opponentBubble") as Sprite;
			new ChatBubble(controller,player,bubble,CHAT_BUBBLE_LIFETIME);
		}
		
		private function onGo(event:Event):void {
			color.gotoAndStop(0);
		}

		private function onAvatar(event:Event):void {
			avatar.addChild(player.avatarBitmap);
		}

		private function warningSoundLoop(event:TimerEvent):void{
			audio.playSound("Time_warning");
		}
		
		private function onPlayerColor(event:ServerMessageEvent):void {
			var colorFrame:String = event.parameters[player.isUser?"userColor":"opponentColor"];
			color.gotoAndStop(colorFrame=="h"?2:3);
		}
		
		private function onHitResultSynchronized(event:ServerMessageEvent):void {
			pocketedballTube.pocketedBalls = player.pocketedBalls;
			physicController.ballReset();
		}
		
		private function onQueueShot(event:Event):void{
			lastTenSeconds.stop();
			lastTenSeconds.reset();		
		}

		private function onActiveChange(event:Event):void {
			time.visible = player.active;
			highlight.visible = player.active;
			Debug.trace("PlayerView:onActiveChange:player.active "+player.active);
			
			if(player.isUser && player.active ){
				audio.playSound("turn_starts");
			}
			
			if(player.active){		
				singleStopWatch.addEventListener(StopWatch.TIMER_TICK, onTimeTick);
			}else{
				singleStopWatch.removeEventListener(StopWatch.TIMER_TICK, onTimeTick);
			}		
		}
		
		private function onTimeTick(event:Event):void {
			if( player.active && player .isUser){
				if(!lastTenSeconds.running && singleStopWatch.remainingSeconds == 10 && singleStopWatch.remainingMinutes == 0){
					lastTenSeconds.start();
				}
			}
			time.text = singleStopWatch.remainingTimeDigital;
		}	
	
	}
}
